package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.*;
import com.yugong.entity.vo.request.StudentRequestVO;
import com.yugong.entity.vo.response.StudentListResponseVO;
import com.yugong.mapper.*;
import com.yugong.service.StudentService;
import com.yugong.util.DigestUtils;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 学生业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/18
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private OrderClassMapper orderClassMapper;
    @Resource
    private OrderClassAdminMapper orderClassAdminMapper;
    @Resource
    private MajorMapper majorMapper;
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private TimeUtils timeUtils;
    @Resource
    private DigestUtils digestUtils;

    /**
     * 根据学生姓名查询学生信息
     *
     * @param pageNum     当前页
     * @param pageSize    当前页的数据个数
     * @param studentName 学生姓名
     * @return 学生信息
     */
    @Override
    public PageInfo<StudentListResponseVO> findStudent(Integer pageNum, Integer pageSize, String studentName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper
                .like(Student::getStudentName, studentName)
                .orderByDesc(Student::getStudentCreateTime);
        List<Student> studentList = studentMapper.selectList(studentLambdaQueryWrapper);
        List<StudentListResponseVO> studentListResponseVOList = new ArrayList<>();
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper
                .like("student_name", studentName);
        Long studentCountNumber = studentMapper.selectCount(studentQueryWrapper);
        for (Student student : studentList) {
            StudentListResponseVO studentListResponseVO = new StudentListResponseVO();
            studentListResponseVO.setStudentNo(student.getStudentNo());
            if ("".equals(student.getStudentName())) {
                studentListResponseVO.setStudentName("未填写");
            } else {
                studentListResponseVO.setStudentName(student.getStudentName());
            }
            studentListResponseVO.setStudentAge(student.getStudentAge());
            if (1 == student.getStudentSex()) {
                studentListResponseVO.setStudentSex("男");
            } else if (0 == student.getStudentSex()) {
                studentListResponseVO.setStudentSex("女");
            } else {
                studentListResponseVO.setStudentSex("未填写");
            }
            if (student.getStudentIntroduction() == null) {
                studentListResponseVO.setStudentIntroduction("未填写");
            } else {
                studentListResponseVO.setStudentIntroduction(student.getStudentIntroduction());
            }
            if (student.getStudentHobby() == null) {
                studentListResponseVO.setStudentHobby("未填写");
            } else {
                studentListResponseVO.setStudentHobby(student.getStudentHobby());
            }
            if (student.getStudentComprehensiveEvaluation() == null) {
                studentListResponseVO.setStudentComprehensiveEvaluation("未填写");
            } else {
                studentListResponseVO.setStudentComprehensiveEvaluation(student.getStudentComprehensiveEvaluation());
            }
            if (0 == student.getStudentOrderClassReviewState()) {
                studentListResponseVO.setStudentOrderClassReviewState("未报名");
            } else if (1 == student.getStudentOrderClassReviewState()) {
                studentListResponseVO.setStudentOrderClassReviewState("未通过");
            } else {
                studentListResponseVO.setStudentOrderClassReviewState("已通过");
            }
            if (student.getStudentBusinessNo() == null) {
                studentListResponseVO.setStudentBusinessNo("未报名");
                studentListResponseVO.setStudentBusinessName("未报名");
            } else {
                studentListResponseVO.setStudentBusinessNo(student.getStudentBusinessNo());
                Business business = businessMapper.selectById(student.getStudentBusinessNo());
                studentListResponseVO.setStudentBusinessName(business.getBusinessName());
            }
            if (0 == student.getStudentEmploymentState()) {
                studentListResponseVO.setStudentEmploymentState("未录用");
            } else {
                studentListResponseVO.setStudentEmploymentState("已录用");
            }
            if (student.getStudentOrderClassNo() == null) {
                studentListResponseVO.setStudentOrderClassNo("未报名");
                studentListResponseVO.setStudentOrderClassName("未报名");
                studentListResponseVO.setStudentOrderClassAdminNo("未报名");
                studentListResponseVO.setStudentOrderClassAdminName("未报名");
                studentListResponseVO.setStudentMajorNo("未报名");
                studentListResponseVO.setStudentMajorName("未报名");
            } else {
                studentListResponseVO.setStudentOrderClassNo(student.getStudentOrderClassNo());
                OrderClass orderClass = orderClassMapper.selectById(student.getStudentOrderClassNo());
                studentListResponseVO.setStudentOrderClassName(orderClass.getOrderClassName());
                studentListResponseVO.setStudentOrderClassAdminNo(orderClass.getOrderClassAdministratorNo());
                OrderClassAdmin orderClassAdmin = orderClassAdminMapper.selectById(orderClass.getOrderClassAdministratorNo());
                studentListResponseVO.setStudentOrderClassAdminName(orderClassAdmin.getOrderClassAdminName());
                studentListResponseVO.setStudentMajorNo(orderClass.getOrderClassMajorNo());
                Major major = majorMapper.selectById(orderClass.getOrderClassMajorNo());
                studentListResponseVO.setStudentMajorName(major.getMajorName());
            }
            studentListResponseVO.setStudentCreateMan(student.getStudentCreateMan());
            String createTime = timeUtils.setDateToStrDate(student.getStudentCreateTime(), Const.YYYY_MM_DD);
            studentListResponseVO.setStudentCreateTime(createTime);
            if (student.getStudentUpdateMan() == null) {
                studentListResponseVO.setStudentUpdateMan("未更新");
            } else {
                studentListResponseVO.setStudentUpdateMan(student.getStudentCreateMan());
            }
            if (student.getStudentUpdateTime() == null) {
                studentListResponseVO.setStudentUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(student.getStudentUpdateTime(), Const.YYYY_MM_DD);
                studentListResponseVO.setStudentUpdateTime(updateTime);
            }
            Account account = accountMapper.selectById(student.getStudentNo());
            if (account == null) {
                studentListResponseVO.setStudentPhone("未更新");
                studentListResponseVO.setStudentEmail("未更新");
            } else {
                if ("".equals(account.getAccountPhone())) {
                    studentListResponseVO.setStudentPhone("未更新");
                } else {
                    studentListResponseVO.setStudentPhone(digestUtils.base64Decode(account.getAccountPhone()));
                }
                if ("".equals(account.getAccountEmail())) {
                    studentListResponseVO.setStudentEmail("未更新");
                } else {
                    studentListResponseVO.setStudentEmail(digestUtils.base64Decode(account.getAccountEmail()));
                }
            }
            studentListResponseVO.setStudentCountNumber(studentCountNumber);
            studentListResponseVOList.add(studentListResponseVO);
        }
        return PageInfo.of(studentListResponseVOList);
    }

    /**
     * 删除学生
     *
     * @param studentNo 学号
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteStudent(String studentNo) {
        int deleteAccountFlag = accountMapper.deleteById(studentNo);
        int deleteStudentFlag = studentMapper.deleteById(studentNo);
        return deleteAccountFlag + deleteStudentFlag;
    }


    /**
     * 查询学生个人信息
     *
     * @param studentNo 学号
     * @return 学生个人信息
     */
    @Override
    public StudentListResponseVO studentInfo(String studentNo) {
        Student student = studentMapper.selectById(studentNo);
        StudentListResponseVO studentListResponseVO = new StudentListResponseVO();
        studentListResponseVO.setStudentNo(studentNo);
        if (student == null) {
            studentListResponseVO.setStudentName("");
            studentListResponseVO.setStudentAge(null);
            studentListResponseVO.setStudentSex("");
            studentListResponseVO.setStudentPhone("");
            studentListResponseVO.setStudentEmail("");
            studentListResponseVO.setStudentOrderClassNo("无");
            studentListResponseVO.setStudentOrderClassName("无");
            studentListResponseVO.setStudentMajorNo("无");
            studentListResponseVO.setStudentMajorName("无");
            studentListResponseVO.setStudentOrderClassReviewState("未报名");
            studentListResponseVO.setStudentEmploymentState("未录用");
            studentListResponseVO.setStudentOrderClassAdminNo("无");
            studentListResponseVO.setStudentOrderClassAdminName("无");
            studentListResponseVO.setStudentBusinessNo("无");
            studentListResponseVO.setStudentBusinessName("无");
        } else {
            if ("".equals(student.getStudentName())) {
                studentListResponseVO.setStudentName("");
            } else {
                studentListResponseVO.setStudentName(student.getStudentName());
            }
            if (0 == student.getStudentAge()) {
                studentListResponseVO.setStudentAge(null);
            } else {
                studentListResponseVO.setStudentAge(student.getStudentAge());
            }
            if (1 == student.getStudentSex()) {
                studentListResponseVO.setStudentSex("男");
            } else if (0 == student.getStudentSex()) {
                studentListResponseVO.setStudentSex("女");
            } else {
                studentListResponseVO.setStudentSex("");
            }
            Account account = accountMapper.selectById(studentNo);
            if (account == null) {
                studentListResponseVO.setStudentPhone("");
                studentListResponseVO.setStudentEmail("");
            } else {
                if (account.getAccountPhone() == null) {
                    studentListResponseVO.setStudentPhone("");
                } else {
                    studentListResponseVO.setStudentPhone(digestUtils.base64Decode(account.getAccountPhone()));
                }
                if (account.getAccountEmail() == null) {
                    studentListResponseVO.setStudentEmail("");
                } else {
                    studentListResponseVO.setStudentEmail(digestUtils.base64Decode(account.getAccountEmail()));
                }
            }
            OrderClass orderClass = orderClassMapper.selectById(student.getStudentOrderClassNo());
            if (orderClass == null) {
                studentListResponseVO.setStudentOrderClassNo("无");
                studentListResponseVO.setStudentOrderClassName("无");
                studentListResponseVO.setStudentMajorNo("无");
                studentListResponseVO.setStudentMajorName("无");
                studentListResponseVO.setStudentOrderClassAdminNo("无");
                studentListResponseVO.setStudentOrderClassAdminName("无");
            } else {
                if (orderClass.getOrderClassNo() == null) {
                    studentListResponseVO.setStudentOrderClassNo("无");
                } else {
                    studentListResponseVO.setStudentOrderClassNo(orderClass.getOrderClassNo());
                }
                if (orderClass.getOrderClassName() == null) {
                    studentListResponseVO.setStudentOrderClassName("无");
                } else {
                    studentListResponseVO.setStudentOrderClassName(orderClass.getOrderClassName());
                }
                Major major = majorMapper.selectById(orderClass.getOrderClassMajorNo());
                if (major == null) {
                    studentListResponseVO.setStudentMajorNo("无");
                    studentListResponseVO.setStudentMajorName("无");
                } else {
                    if (major.getMajorNo() == null) {
                        studentListResponseVO.setStudentMajorNo("无");
                    } else {
                        studentListResponseVO.setStudentMajorNo(major.getMajorNo());
                    }
                    if (major.getMajorName() == null) {
                        studentListResponseVO.setStudentMajorName("无");
                    } else {
                        studentListResponseVO.setStudentMajorName(major.getMajorName());
                    }
                }
                OrderClassAdmin orderClassAdmin = orderClassAdminMapper.selectById(orderClass.getOrderClassAdministratorNo());
                if (orderClassAdmin == null) {
                    studentListResponseVO.setStudentOrderClassAdminNo("无");
                    studentListResponseVO.setStudentOrderClassAdminName("无");
                } else {
                    if (orderClassAdmin.getOrderClassAdminNo() == null) {
                        studentListResponseVO.setStudentOrderClassAdminNo("无");
                    } else {
                        studentListResponseVO.setStudentOrderClassAdminNo(orderClassAdmin.getOrderClassAdminNo());
                    }
                    if (orderClassAdmin.getOrderClassAdminName() == null) {
                        studentListResponseVO.setStudentOrderClassAdminName("无");
                    } else {
                        studentListResponseVO.setStudentOrderClassAdminName(orderClassAdmin.getOrderClassAdminName());
                    }
                }
            }
            if (0 == student.getStudentOrderClassReviewState()) {
                studentListResponseVO.setStudentOrderClassReviewState("未报名");
            } else if (1 == student.getStudentOrderClassReviewState()) {
                studentListResponseVO.setStudentOrderClassReviewState("已报名");
            } else if (2 == student.getStudentOrderClassReviewState()) {
                studentListResponseVO.setStudentOrderClassReviewState("未通过");
            } else {
                studentListResponseVO.setStudentOrderClassReviewState("通过");
            }
            if (0 == student.getStudentEmploymentState()) {
                studentListResponseVO.setStudentEmploymentState("未录用");
            } else {
                studentListResponseVO.setStudentEmploymentState("已录用");
            }
            Business business = businessMapper.selectById(student.getStudentBusinessNo());
            if (business == null) {
                studentListResponseVO.setStudentBusinessNo("无");
                studentListResponseVO.setStudentBusinessName("无");
            } else {
                if (business.getBusinessNo() == null) {
                    studentListResponseVO.setStudentBusinessNo("无");
                } else {
                    studentListResponseVO.setStudentBusinessNo(business.getBusinessNo());
                }
                if (business.getBusinessName() == null) {
                    studentListResponseVO.setStudentBusinessName("无");
                } else {
                    studentListResponseVO.setStudentBusinessName(business.getBusinessName());
                }
            }
        }
        return studentListResponseVO;
    }

    /**
     * 修改信息
     *
     * @param studentRequestVO 学生请求实体类
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateInfo(StudentRequestVO studentRequestVO) {
        Date updateTime = new Date();
        Account account = new Account();
        account.setAccountNo(studentRequestVO.getStudentNo());
        account.setAccountPhone(digestUtils.base64Encode(studentRequestVO.getStudentPhone()));
        account.setAccountEmail(digestUtils.base64Encode(studentRequestVO.getStudentEmail()));
        account.setAccountUpdateMan(studentRequestVO.getStudentNo());
        account.setAccountUpdateTime(updateTime);
        int updateAccountFlag = accountMapper.updateById(account);
        Student student = new Student();
        student.setStudentNo(studentRequestVO.getStudentNo());
        student.setStudentName(studentRequestVO.getStudentName());
        student.setStudentAge(studentRequestVO.getStudentAge());
        student.setStudentSex(studentRequestVO.getStudentSex());
        student.setStudentUpdateMan(studentRequestVO.getStudentNo());
        student.setStudentUpdateTime(updateTime);
        int updateStudentFlag = studentMapper.updateById(student);
        return updateAccountFlag + updateStudentFlag;
    }

    /**
     * 学生报名
     *
     * @param studentRequestVO 学生请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int studentJoin(StudentRequestVO studentRequestVO) {
        Student student = new Student();
        student.setStudentNo(studentRequestVO.getStudentNo());
        student.setStudentOrderClassReviewState(1);
        student.setStudentIntroduction(studentRequestVO.getStudentIntroduction());
        student.setStudentHobby(studentRequestVO.getStudentHobby());
        student.setStudentOrderClassNo(studentRequestVO.getStudentOrderClassNo());
        student.setStudentUpdateMan(studentRequestVO.getStudentNo());
        student.setStudentUpdateTime(new Date());
        return studentMapper.updateById(student);
    }

    /**
     * 审核学生列表
     *
     * @param pageNum           当前页
     * @param pageSize          当前页数据个数
     * @param orderClassName    班名
     * @param orderClassAdminNo 订单班管理员工号
     * @return 审核学生列表
     */
    @Override
    public PageInfo<StudentListResponseVO> checkStudentPage(Integer pageNum, Integer pageSize, String orderClassName, String orderClassAdminNo) {
        PageHelper.startPage(pageNum, pageSize);
        // 查询订单班管理员自己的订单班
        LambdaQueryWrapper<OrderClass> orderClassLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderClassLambdaQueryWrapper
                .eq(OrderClass::getOrderClassAdministratorNo, orderClassAdminNo)
                .orderByDesc(OrderClass::getOrderClassCreateTime);
        List<OrderClass> orderClassList = orderClassMapper.selectList(orderClassLambdaQueryWrapper);
        List<StudentListResponseVO> studentListResponseVOList = new ArrayList<>();
        Long total = 0L;
        for (OrderClass orderClass : orderClassList) {
            if (orderClass == null) {
                return null;
            } else {
                if (orderClass.getOrderClassNo() == null) {
                    return null;
                } else {
                    // 查询报名自己订单班的学生信息
                    LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    studentLambdaQueryWrapper
                            .ne(Student::getStudentOrderClassReviewState, 0)
                            .eq(Student::getStudentOrderClassNo, orderClass.getOrderClassNo())
                            .orderByDesc(Student::getStudentCreateTime);
                    QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
                    studentQueryWrapper.eq("student_order_class_no", orderClass.getOrderClassNo());
                    Long countNumber = studentMapper.selectCount(studentQueryWrapper);
                    total = total + countNumber;
                    List<Student> studentList = studentMapper.selectList(studentLambdaQueryWrapper);
                    for (Student student : studentList) {
                        StudentListResponseVO studentListResponseVO = new StudentListResponseVO();
                        studentListResponseVO.setStudentNo(student.getStudentNo());
                        studentListResponseVO.setStudentName(student.getStudentName());
                        studentListResponseVO.setStudentIntroduction(student.getStudentIntroduction());
                        studentListResponseVO.setStudentHobby(student.getStudentHobby());
                        LambdaQueryWrapper<OrderClass> orderClassWrapper = new LambdaQueryWrapper<>();
                        orderClassWrapper
                                .eq(OrderClass::getOrderClassNo, student.getStudentOrderClassNo())
                                .like(OrderClass::getOrderClassName, orderClassName);
                        OrderClass oc = orderClassMapper.selectOne(orderClassWrapper);
                        if (oc == null) {
                            continue;
                        } else {
                            studentListResponseVO.setStudentOrderClassNo(oc.getOrderClassNo());
                            studentListResponseVO.setStudentOrderClassName(oc.getOrderClassName());
                        }
                        if (1 == student.getStudentOrderClassReviewState()) {
                            studentListResponseVO.setStudentOrderClassReviewState("已报名");
                        } else if (2 == student.getStudentOrderClassReviewState()) {
                            studentListResponseVO.setStudentOrderClassReviewState("未通过");
                        } else {
                            studentListResponseVO.setStudentOrderClassReviewState("已通过");
                        }
                        studentListResponseVO.setStudentCountNumber(total);
                        studentListResponseVOList.add(studentListResponseVO);
                    }
                }
            }
        }
        return PageInfo.of(studentListResponseVOList);
    }

    /**
     * 审核学生
     *
     * @param studentRequestVO 学生请求实体类
     * @return 响应码
     */
    @Override
    public int checkStudent(StudentRequestVO studentRequestVO) {
        Student student = new Student();
        student.setStudentNo(studentRequestVO.getStudentNo());
        student.setStudentOrderClassReviewState(studentRequestVO.getStudentOrderClassReviewState());
        student.setStudentUpdateMan(studentRequestVO.getStudentUpdateMan());
        student.setStudentUpdateTime(new Date());
        if (2 != studentRequestVO.getStudentOrderClassReviewState()) {
            Student stu = studentMapper.selectById(studentRequestVO.getStudentNo());
            OrderClass orderClass = orderClassMapper.selectById(stu.getStudentOrderClassNo());
            student.setStudentBusinessNo(orderClass.getOrderClassBusinessNo());
        }
        return studentMapper.updateById(student);
    }

    /**
     * 学生查看个人评价
     *
     * @param studentNo 学号
     * @return 个人评价
     */
    @Override
    public String evaluateStudent(String studentNo) {
        Student student = studentMapper.selectById(studentNo);
        if (student.getStudentComprehensiveEvaluation() == null) {
            return "无";
        } else {
            return student.getStudentComprehensiveEvaluation();
        }
    }
}
